package com.cogwheel.framework.graphics;

import javax.media.opengl.GL2;

import com.cogwheel.framework.init.CPreferences;
import com.cogwheel.framework.util.CRectangle;
import com.cogwheel.framework.util.input.CKeyboard;
import com.cogwheel.framework.util.input.CMouse;

public class CAnim extends CSprite{
	public int SpriteCount = 0;
	public int CurrentFrame = 0;
	private long lTime = 0L;
	
	public CAnim(CPreferences preferences, String spriteID, CRectangle dimensions, int sprites) {
		super(preferences, spriteID, dimensions);
		SpriteCount = sprites;
	}
	
	@Override
	public void CUpdate(CMouse mouse, CKeyboard keyboard){
		if (System.currentTimeMillis() - lTime >= Preferences.ANIMATION_SPEED){
			if (CurrentFrame < SpriteCount - 1){ CurrentFrame++; }
			else{ CurrentFrame = 0; }
		
			lTime = System.currentTimeMillis();
		} 
	}
	
	@Override
	protected void CDraw2D(GL2 gl){
		if (TextureID != "NULL" && TextureID != null){	
			gl.glTranslatef(xa, ya, 1f);
		
			gl.glBegin(GL2.GL_QUADS);
				gl.glTexCoord2f(CurrentFrame * (1f / SpriteCount), 1.0f); //Upper left
				gl.glVertex2f(-w/SpriteCount, h);
		
				gl.glTexCoord2f((CurrentFrame + 1 )* (1f / SpriteCount), 1.0f); //Upper right
				gl.glVertex2f(w/SpriteCount, h);
		
				gl.glTexCoord2f((CurrentFrame + 1) * (1f / SpriteCount), 0.0f); //Bottom right
				gl.glVertex2f(w/SpriteCount, -h);
		
				gl.glTexCoord2f(CurrentFrame * (1f / SpriteCount), 0.0f); //Bottom left
				gl.glVertex2f(-w/SpriteCount, -h);
			gl.glEnd();
		}
	}
}
